import { Model } from "./model";
import { Tools } from "./tools";

export class Ball extends Model {

    constructor(context, x, y, r) {
        super(context, context.collections.ball, x, y, r);
        this.running = false;
        this.speed = 3;
        this.start();
    }

    setPList(plist, index) {
        // 目标点列表 plist = [{x0, y0}, {x1, y1}...]
        this.plist = plist;
        // 当前目标点
        this.p = index;
        this.pdirection = 1; // 方向
        /* setTimeout(() => {
            this.pdirection = -1;
        }, 5000) */
    }

    setHeader(bo) {
        this.isHeader = bo;
    }
    setTail(bo) {
        this.isTail = bo;
    }

    delete() {
        this.prev.setNext(this.next);
        this.next.setPrev(this.prev);
    }

    die() {
        this.delete();
        this.destroy();
    }

    start() {
        this.running = true;
        this.registerRepeat();
        this.registerDrawable();
    }

    setPrev(model) {
        this.prev = model;
    }

    setNext(model) {
        this.next = model;
    }

    repeat() {
        if (this.next) {
            if (Tools.hitTest(this, this.next)) {
                let currentPoint = this.plist[this.p];
                if (currentPoint) {
                    const dx = currentPoint.x - this.x;
                    const dy = currentPoint.y - this.y;
                    const distance = Math.sqrt(dx * dx + dy * dy);
                    const speed = this.speed;
                    if (distance > speed) {
                        const angle = Math.atan2(dy, dx);
                        this.x += speed * Math.cos(angle);
                        this.y += speed * Math.sin(angle);
                        let temp = this;
                        while (temp.prev) {
                            temp = temp.prev;
                            if (temp.repeat) {
                                temp.repeat();
                            }
                        }
                    } else {
                        this.x = currentPoint.x;
                        this.y = currentPoint.y;
                        this.p = (this.p + this.pdirection) % this.plist.length;
                    }
                }
            }
        }
        
       /*  if (this.isHeader) {
            let currentPoint = this.plist[this.p];
            if (currentPoint) {
                const dx = currentPoint.x - this.x;
                const dy = currentPoint.y - this.y;
                const distance = Math.sqrt(dx * dx + dy * dy);
                const speed = 3;
                if (distance > speed) {
                    const angle = Math.atan2(dy, dx);
                    this.x += speed * Math.cos(angle);
                    this.y += speed * Math.sin(angle);
                } else {
                    this.x = currentPoint.x;
                    this.y = currentPoint.y;
                    // Move to the next point if available
                    this.p = (this.p + this.pdirection) % this.plist.length;
                }
            }
            return;
        }
        this.currentTarget = this.prev;
        if (this.currentTarget && !Tools.hitTest(this, this.currentTarget)) {
            let prevPoint = { x: this.currentTarget.x, y: this.currentTarget.y };
            const dx = prevPoint.x - this.x;
            const dy = prevPoint.y - this.y;
            const distance = Math.sqrt(dx * dx + dy * dy);
            const speed = 3;
            if (distance > speed) {
                const angle = Math.atan2(dy, dx);
                this.x += speed * Math.cos(angle);
                this.y += speed * Math.sin(angle);
            } else {
                this.x = prevPoint.x;
                this.y = prevPoint.y;
            }
        } */
    }

    draw(ctx2d) {
        if (this.running) {
            ctx2d.beginPath();
            ctx2d.fillStyle = 'white';
            ctx2d.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
            ctx2d.fillStyle = 'red';
            if (this.isHeader) {
                ctx2d.fillStyle = 'white';
            }
            if (this.isTail) {
                ctx2d.fillStyle = 'blue';
            }
            ctx2d.strokeStyle = 'white';
            ctx2d.lineWidth = 2;
            ctx2d.stroke();
            ctx2d.fill();
            ctx2d.closePath();
        }
    }

}